//-----------------------------------------------------------------------
// <copyright file="UtilityDAO.cs" company="Scb">
//     Copyright ? Scb. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;

namespace Robot.Data
{    
    class UtilityDAO
    {

        public static string EscapeString(string sourceString)
        {
            if (string.IsNullOrEmpty(sourceString))
                return null;
            return sourceString.Trim()
            .Replace(@"/", @"//")
            .Replace("'", "''")
            .Replace("%", @"/%")
            .Replace("[", @"/[");
        }

        public static string BuildFullIndexSearchString(string str)
        {
            if (str == null)
                return str;
            // make string standard "a b c d"
            str = str.Trim();
            while (str.IndexOf("  ") >= 0) str = str.Replace("  ", " ");

            // escape " by ""
            str = str.Replace("\"", "\"\"")
                // replace [space] by [" AND "]
                    .Replace(" ", "\" AND \"")
                // replace ' by ''
                    .Replace("'", "''");

            if (str != string.Empty)
                //wrap string by " "
                return string.Format("\"{0}\"", str);

            return str;
        }

        public static bool CheckColumnData(IDataReader reader, string name)
        {

            int ordinal = GetOrdinal(reader, name);
            if (ordinal >= 0 && !reader.IsDBNull(ordinal))
            {
                return true;
            }
            return false;
        }

        public static int GetOrdinal(IDataReader reader, string name)
        {
            try
            {
                return reader.GetOrdinal(name);
            }
            catch (IndexOutOfRangeException)
            {
                return -1;
            }
        }
       
    }
}